<template>
  <div>
    <a-card :bordered="false">
      <f-table ref="table" size="default" rowKey="menuId" :columns="columns" :treeTable="true" :api="api.menu" :multiple="false" :showPagination="false" :operator="operator" :showIndex="false">
         <span slot="menuName" slot-scope="text, record, index">
          &nbsp; <a-icon :type="record.icon"/> &nbsp;{{record.menuName}}
         </span>
        <span slot="status" slot-scope="text, record, index">
           <a-switch v-model="record.status==1" checkedChildren="启用" unCheckedChildren="禁用" @change="(checked) => setStatus(checked,record)"/>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="$refs.modal.handleShow(record)">编辑</a>
            <a-divider type="vertical"/>
          </template>
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <a-menu-item v-if="$auth('table.delete')">
                <a href="javascript:;" @click="$refs.table.remove(record.menuId)">删除</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </f-table>
    </a-card>
    <menus-modal ref="modal"></menus-modal>
  </div>
</template>

<script>
  import { listConvertTree } from '@/utils/routerUtil'
  import MenusModal from './menus_modal'

  export default {
    name: 'TableList',
    components: { MenusModal },
    data () {
      return {
        load: false,
        selectMenus: [],
        columns: [
          {
            title: '菜单名称',
            align: 'left',
            width: 180,
            scopedSlots: { customRender: 'menuName' }
          },
          {
            title: '编码',
            width: 150,
            align: 'center',
            dataIndex: 'menuCode'
          },
          {
            title: '路径',
            align: 'center',
            width: 150,
            dataIndex: 'path'
          },

          {
            title: '状态',
            align: 'center',
            width: 150,
            scopedSlots: { customRender: 'status' }
          },
          {
            title: '操作',
            width: 250,
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' }
          }
        ],
        //操作按钮
        operator: [
          {
            name: '添加菜单',
            icon: 'plus',
            that: this,
            handler: function (table, that) {
              that.$refs.modal.handleShow()
            }
          },
          {
            name: '删除',
            icon: 'delete',
            batch: true,
            handler: function (table) {
              table.remove()
            }
          }
        ]
      }
    },
    created () {
      //this.getData()
    },
    methods: {
      setStatus (check, record) {
        this.http.post(this, {
          url: this.api.menu.setState,
          data: {
            ids: record.menuId,
            status: Number(check)
          },
          success (me, data) {
            record.status = data
          }
        })
      },
      reload () {
        //this.getData()
      },
      getData () {
        let me = this
        new Promise((resolve, reject) => {
          this.http.get(this, {
            url: this.api.menu.list,
            data: {},
            success: function (me, data) {
              let opt = {
                primaryKey: 'menuId',
                parentKey: 'parentId',
                startPid: '0'
              }
              me.selectMenus = listConvertTree(data, opt)
              me.load = true
              resolve(true)
            }
          })
        }).then(res => {
          me.$refs.table.loadData()
        })
      }
    }
  }
</script>
